<?php

namespace Admin\Model\DBO;

use Zend\Db\TableGateway\AbstractTableGateway;

class StatisticsDBO extends AbstractTableGateway {

    private $sm;
    private $ciculationTable;
    private $serialBookTable;
    public function __construct($serviceManager) {
        $this->sm = $serviceManager;
    }

    public function getCiculationTable() {
        if (!$this->ciculationTable) {
            $this->ciculationTable = $this->sm->get('Admin\Model\DAO\CiculationTable');
        }
        return $this->ciculationTable;
    }

    public function getSerialBookTable() {
        if (!$this->serialBookTable) {
            $this->serialBookTable = $this->sm->get('Admin\Model\DAO\SerialBookTable');
        }
        return $this->serialBookTable;
    }

    public function statisticsDocumentBorrowing() {
        $resultSet = $this->getCiculationTable()->fetchAll();
        $objectSearchBook = new SearchBookDBO($this->sm);
        $allAuthor = array();
        // Lấy toàn bộ tác giả của cuốn sách
        foreach ($resultSet as $key) {
            $stringAuthor = "";
            $authors = $objectSearchBook->searchBookAuthor($key['bookBarcode']);
            foreach ($authors as $author) {
                $stringAuthor .= $author['author'] . '-';
            }
            $allAuthor[$key['bookBarcode']] = $stringAuthor;
        }
        $iteratorAdapter = new \Zend\Paginator\Adapter\Iterator($this->getCiculationTable()->fetchAll());
        $paginator = new \Zend\Paginator\Paginator($iteratorAdapter);
        return array(
            'paginator' => $paginator,
            'allAuthor' => $allAuthor,
        );
    }
    public function statisticsDocumentInStore() {
        $resultSet = $this->getSerialBookTable()->statisticsDocumentInStore();
        $objectSearchBook = new SearchBookDBO($this->sm);
        $allAuthor = array();
        // Lấy toàn bộ tác giả của cuốn sách
        foreach ($resultSet as $key) {
            $stringAuthor = "";
            $authors = $objectSearchBook->searchBookAuthor($key['bookBarcode']);
            foreach ($authors as $author) {
                $stringAuthor .= $author['author'] . '-';
            }
            $allAuthor[$key['bookBarcode']] = $stringAuthor;
        }
        $iteratorAdapter = new \Zend\Paginator\Adapter\Iterator($this->getSerialBookTable()->statisticsDocumentInStore());
        $paginator = new \Zend\Paginator\Paginator($iteratorAdapter);
        return array(
            'paginator' => $paginator,
            'allAuthor' => $allAuthor,
        );
    }
}